import { SEND_VERIFY_CODE } from "../api";

export default {
  data(){
    return {
      textTip:'',
      invalid:false,
      isSend:false,
      wait:60
    }
  },
  methods:{
    onVerifiCodeBtn(){
        //倒计时countdown
        if(this.isSend){
            return false;
        }
        this.isSend = true;
        var wait = this.wait;
        var countdown = ()=>{
            if (wait == 0) {
                this.textTip="重新获取";
                this.invalid= false;
                this.isSend = false;
                this.wait = 60;
            } else {
                this.textTip = wait + 's后重试';
                wait--;
                setTimeout(countdown, 1000);
            }
        };
        this.invalid=true;
        countdown();
    },
    onGetVerificCode(){
        if(this.invalid){
          return;
        }
        //telephone
        if(!this.phone_number){
            this.$toast.show('请填写手机号码');
            return false;
        }

        if(!/^1[3,5,7,8]\d{9}$/i.test(this.phone_number)){
            this.$toast.show('手机号码格式有误');
            return false;
        }
        this.invalid = true;
       // console.log(this.invalid)
        this.$http.get({
            url:SEND_VERIFY_CODE,
            data:{
                phone_number:this.phone_number,
            }
        }).then((response)=>{
            var result = response.data;
            if (result.code === '0') {
                this.onVerifiCodeBtn();
            }else{
                this.$toast.show(result.message);
                this.invalid = false;
            }
        },()=>{
          this.invalid = false;
        });
    }
  }
}
